<!DOCTYPE html>
<html>

    <meta charset="utf-8">

    <head th:include="include :: header"></head>

    <body>

        <div class="ibox-content">
            <div id="menuTree"></div>
        </div>
        <div class="form-group hidden">
            <div class="col-sm-12 col-sm-offset-12">
                <button type="submit" class="btn btn-primary">确认</button>
            </div>
        </div>

        <div th:include="include :: footer"></div>

        <script type="text/javascript">
            // var ctx = "[[${#request.getContextPath()}]]";
            $(document).ready(function () {
                getTreeData()
            });

            function getTreeData() {
                $.ajax({
                    type: "GET",
                    url: top.ctx + "/sys/menu/list",
                    success: function (data) {
                        loadTree(convert2Tree(data));
                    }
                });
            }

            function loadTree(tree) {
                $('#menuTree').jstree({
                    'core': {
                        'data': tree
                    },
                    "plugins": ["search"]
                });
                $('#menuTree').jstree().open_all();
            }

            $('#menuTree').on("changed.jstree", function (e, data) {
                if (data.node.id != -1) {
                    parent.loadMenu(data.node.id, data.node.text);
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    parent.layer.close(index);
                }

            });

            function convert2Tree(data) {
                var tree = newChildren('0', '根目录', true);
                var parentIdMap = new Object();
                for (var i in data) {
                    if (parentIdMap[data[i].parentId] == null) {
                        parentIdMap[data[i].parentId] = new Array();
                    }
                    var child = newChildren(data[i].menuId, data[i].name);
                    parentIdMap[data[i].parentId].push(child);
                }
                children(tree, parentIdMap)
                return tree;
            }

            function children(treeNode, map) {
                treeNode.children = map[treeNode.id];
                if (treeNode.children != null && treeNode.children.length != 0) {
                    for (var i in treeNode.children) {
                        children(treeNode.children[i], map)
                    }
                }
            }

            function newChildren(id, text, opened) {
                return {
                    "id": id,
                    "text": text,
                    "state": {
                        "opened": opened != null ? opened : false
                    },
                    "checked": false,
                    "attributes": null,
                    "children": [],
                    "parentId": ""
                };
            }
        </script>
    </body>

</html>
